from django.shortcuts import render

from user.models import *
from user.serializers import *

from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from rest_framework import status
from rest_framework.response import Response


# 消息消息查看（用户和用户）
class MessageView(APIView):

    # 发送消息
    def post(self, request):
        try:
            user_id = request.data.get('user_id')
            content = request.data.get("content")
            receive = request.data.get("receive")
            print("接收", request.data)

            data = {
                "user_id":user_id,
                "content":content,
                "receive":receive
            }
            ser = MessageSerializers(data=data)
            if ser.is_valid():
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "错误","data": ser.errors})

            MessageModelModel.objects.create(user_id=user_id, content=content, receive=receive)
            return Response({"cdoe": status.HTTP_200_OK, "msg":"发送成功"})
        except Exception as e:
            print(e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"错误"})

    # 消息查看
    def get(self, request):
        try:
            user_id = request.data.get("user_id")

            user = UserModel.objects.filter(id=user_id).first()
            if user == None:
                return Response({"code":status.HTTP_400_BAD_REQUEST, "msg":"该用户不存在"})
            user_ser = UserSerializers(user).data
            receive = MessageModelModel.objects.filter(receive=user_ser.get("user_name")).all()
            receive_ser = MessageSerializers(receive, many=True).data
            return Response({"code": status.HTTP_200_OK, "msg":"成功", "data": receive_ser})
        except Exception as e:
            print(e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"错误"})


# 评论（内容和用户， 回复（用户和））
class CommentsView(APIView):

    # 发送评论和回复
    def post(self, request):
        try:
            user_id = request.data.get("user_id")
            content = request.data.get("content")
            cont = request.data.get("cont")
            obse_id = request.data.get("obse_id")
            print("接收", request.data)

            data = {
                "user_id":user_id,
                "content":content,
                "cont":cont,
                "obse_id":obse_id
            }

            ser = ObserverSerializers(data=data)
            if ser.is_valid():
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "错误","data": ser.errors})

            if ObserverModel.objects.filter(user_id=user_id, content=content).count() != 0:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"您已经评论过相同的内容不允许再次评论相同的内容"})

            if int(obse_id) == 0:
                print("无》》》》》》》》》》》》》》")
                ObserverModel.objects.create(user_id=user_id, content=content, cont_id=cont)
                return Response({"code": status.HTTP_200_OK, "msg":"发送成功"})
            print("有》》》》》》》》》》》》》》》")
            ObserverModel.objects.create(user_id=user_id, content=content, obse_id=obse_id)
            return Response({"code": status.HTTP_200_OK, "msg":"回复成功"})
        except Exception as e:
            print(e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"错误"})


class CommentViewsTow(APIView):
    # 查看
    def get(self, request, id):
        try:
            observer = ObserverModel.objects.filter(cont=id, is_delete=0).first()
            observer_ser = ObserverSerializers(observer).data

            print(observer_ser.get("id"))
            print(observer_ser)
            obse_all = ObserverModel.objects.filter(obse_id=observer_ser.get("id")).all()
            obse_all_ser = ObserverSerializers(obse_all, many=True).data
            observer_ser["obse"] = obse_all_ser
            print(obse_all_ser)
            return Response({"网上调查v吧": status.HTTP_200_OK,"msg": "查询", 'data': observer_ser})
        except Exception as e:
            print(e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"错误"})


# 雪花算法（时间回拨）
# 索引








